from utils.HttpUtils import HttpUtils

v

import requests
from lxml import etree
from fake_useragent import UserAgent
from http import cookiejar
import json
from datetime import datetime
import time

"""
京东商品评论
只能查看前100页
参数callback、productId 需要修改
"""
class JD():
    def __init__(self):
        #声明一个CookieJar对象实例来保存cookie
        self.cookie = cookiejar.CookieJar()
        ua = UserAgent(use_cache_server=False)  # 禁用服务器缓存
        self.headers = {
            "User-Agent": ua.random,
            "referer": "https://item.jd.com/1670646.html"
        }

    def get_comments(self, page):
        postData = {
            "callback": "fetchJSON_comment98vv4002",
            "productId": "1670646",
            "score": "0",
            "sortType": "5",
            "page": page, #"0",
            "pageSize": "10",
            "isShadowSku": "0",
            "fold": "1"
        }
        # url = "https://sclub.jd.com/comment/productPageComments.action?callback=fetchJSON_comment98vv3087&productId=1670646&score=0&sortType=5&page=0&pageSize=10&isShadowSku=0&fold=1"
        url = "https://club.jd.com/comment/productPageComments.action"
        time.sleep(1)
        html = HttpUtils.do_request("GET", url, self.headers, postData)
        # html.encoding = 'utf-8'
        # selector = etree.HTML(html.text)
        aa = html.text.replace("fetchJSON_comment98vv4002", "").replace("(", "").replace(")", "").replace(";", "")
        # datajson = json.loads(html.text)
        datajson = json.loads(aa)
        # 评价标签汇总-买家印象
        for h in datajson['hotCommentTagStatistics']:
            dict_data = dict()
            dict_data['name'] = h['name'] # 汇总标签内容
            dict_data['count'] = h['count'] # 汇总标签数量

        # 评价数量
        pj_dict = dict()
        pj_dict['commentCount'] = datajson['productCommentSummary']['commentCount'] # 总评价数量
        pj_dict['videoCount'] = datajson['productCommentSummary']['videoCount'] # 视频评价数量
        pj_dict['afterCount'] = datajson['productCommentSummary']['afterCount'] # 追评数量
        pj_dict['goodCount'] = datajson['productCommentSummary']['goodCount'] # 好评数量
        pj_dict['generalCount'] = datajson['productCommentSummary']['generalCount'] # 中评数量
        pj_dict['poorCount'] = datajson['productCommentSummary']['poorCount'] # 差评数量
        pj_dict['goodRate'] = datajson['productCommentSummary']['goodRate'] # 好评百分比
        pj_dict['generalRate'] = datajson['productCommentSummary']['generalRate'] # 中评百分比
        pj_dict['poorRate'] = datajson['productCommentSummary']['poorRate'] # 差评百分比

        # 用户评价
        for c in datajson['comments']:
            dict_data = dict()
            dict_data['content'] = c['content'] # 评论内容
            dict_data['creationTime'] = c['creationTime'] # 评论时间
            dict_data['nickname'] = c['nickname'] # 用户昵称
            dict_data['productColor'] = c['productColor'] # 购买商品具体哪款
            dict_data['referenceName'] = c['referenceName'] # 购买商品
            dict_data['productSize'] = c['productSize'] # 购买数量
            dict_data['anonymousFlag'] = c['anonymousFlag'] # PLUS会员：1：是，0：否
            dict_data['score'] = c['score'] # 用户评分


if __name__ == '__main__':
    jd = JD()
    for i in range(99, 101):
        jd.get_comments(i)